﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public partial class LeagueScene : Scene
{
	const string UNION_MEMBER_INFO_CELL_NAME = "Cell - Union Member Info";

	public UILabel unionNameLabel;
	public UILabel unionMasterNameLabel;
	public UILabel unionMemberCountLabel;
	public UILabel myInfoNameLabel;
	public UILabel myInfoClassLabel;
	public UILabel myInfoContributionPointLabel;

	//public GameObject emptyListObject;
	public GameObject unionMemberInfoCell;
	public UIGrid unionMemberInfoGrid;
	
	Dictionary<int, UnionMember> unionMemberInfoList = new Dictionary<int, UnionMember>();
	Dictionary<int, GameObject> unionMemberInfoObjectList = new Dictionary<int, GameObject>();
	
	public override void StartScene()
	{
		NetworkManager.Instance.C2S_UNION_GET(UserData.Instance.userModel.sectorUnion, ( responseData ) => {
			
			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopup(responseData.ecode);
				return;
			}
			
			GameObject g = null;
			int idx = 0;
			
			if (responseData.data.unionMemberList.Count <= 0)
			{
				//NGUITools.SetActive(emptyListObject, true);
				return;
			}
			
			//NGUITools.SetActive(emptyListObject, false);

			unionNameLabel.text = responseData.data.name;
			unionMemberCountLabel.text = string.Format("{0} / 99", responseData.data.memberCount.ToString());

			foreach (UnionMember um in responseData.data.unionMemberList)
			{
				// 연합장의 이름 설정 (아래 나의 정보와의 순서 바뀌면 안됨)
				if (um.type == 1)
				{
					unionMasterNameLabel.text = um.name;
				}

				// 나의 정보일 경우
				if (um.id == UserData.Instance.userModel.id)
				{
					myInfoNameLabel.text = um.name;
					myInfoClassLabel.text =  string.Format("[{0}]", GetClassNameByType(um.type));
					myInfoContributionPointLabel.text = string.Format("{0:n0} 점", um.contributionPoint.ToString());
					continue;
				}

				g = NGUITools.AddChild(unionMemberInfoGrid.transform.gameObject, unionMemberInfoCell);
				g.transform.localScale = Vector3.one;
				g.name = UNION_MEMBER_INFO_CELL_NAME;
				
				UnionMemberInfoCell umic = g.GetComponent<UnionMemberInfoCell>();
				umic.indexPath = idx;
				
				umic.nameLabel.text = um.name;
				umic.contributionPointLabel.text = string.Format("{0:n0} 점", um.contributionPoint.ToString());
				umic.classLabel.text =  string.Format("[{0}]", GetClassNameByType(um.type));

				unionMemberInfoObjectList.Add(idx, g);
				unionMemberInfoList.Add(idx, um);
				
				NGUITools.SetActive(g, true);
				
				idx++;
			}

			unionMemberInfoGrid.Reposition();
		});
	}
}
